package com.open.cloud.rbac.service.orgpost.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.cloud.rbac.entity.orgpost.CorePost;
import com.open.cloud.rbac.entity.orgpost.CoreRolePostRel;
import com.open.cloud.rbac.entity.orgpost.CoreUserPostRel;
import com.open.cloud.rbac.mapper.orgpost.CorePostMapper;
import com.open.cloud.rbac.mapper.orgpost.CoreRolePostRelMapper;
import com.open.cloud.rbac.mapper.orgpost.CoreUserPostRelMapper;
import com.open.cloud.rbac.object.orgpost.CorePostDto;
import com.open.cloud.rbac.object.orgpost.CorePostSelectDto;
import com.open.cloud.rbac.service.orgpost.ICorePostService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Service
public class CorePostServiceImpl extends ServiceImpl<CorePostMapper, CorePost>
        implements ICorePostService {

    @Resource
    private CorePostMapper postMapper;

    @Resource
    private Snowflake snowflake;

    @Resource
    private CoreUserPostRelMapper userPostRelMapper;

    @Resource
    private CoreRolePostRelMapper rolePostRelMapper;

    /**
     * 新增岗位
     *
     * @param postDto
     * @return
     */
    @Override
    public JSONObject addPostInfo(CorePostDto postDto) {
        CorePost corePost = new CorePost();
        BeanUtils.copyProperties(postDto, corePost);
        corePost.setId(snowflake.nextIdStr());
        corePost.setCreateTime(new Date());
        corePost.setUpdateTime(new Date());
        int i = postMapper.insert(corePost);
        JSONObject result = new JSONObject();
        if (i > 0) {
            result.put("code", 200);
            result.put("message", "创建成功");
        } else {
            result.put("code", 500);
            result.put("message", "创建失败");
        }
        return result;
    }

    /**
     * 更新岗位
     *
     * @param postDto
     * @return
     */
    @Override
    public JSONObject updatePostInfo(CorePostDto postDto) {
        CorePost corePost = new CorePost();
        BeanUtils.copyProperties(postDto, corePost);
        corePost.setUpdateTime(new Date());
        int i = postMapper.updateById(corePost);
        JSONObject result = new JSONObject();
        if (i > 0) {
            result.put("code", 200);
            result.put("message", "修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "修改失败");
        }
        return result;
    }

    /**
     * 删除岗位
     *
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deletePostInfo(String postId) {
        LambdaQueryWrapper userRelQuery = Wrappers.<CoreUserPostRel>lambdaQuery()
                .eq(CoreUserPostRel::getPostId, postId);
        userPostRelMapper.delete(userRelQuery);

        LambdaQueryWrapper roleRelQuery = Wrappers.<CoreRolePostRel>lambdaQuery()
                .eq(CoreRolePostRel::getPostId, postId);
        rolePostRelMapper.delete(roleRelQuery);

        JSONObject result = new JSONObject();

        int i = this.baseMapper.deleteById(postId);
        if (i > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }

    /**
     * 分页查询岗位
     *
     * @param postSelectDto
     * @return
     */
    @Override
    public IPage<CorePost> selectPostPage(CorePostSelectDto postSelectDto) {

        LambdaQueryWrapper postQuery = Wrappers.<CorePost>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(postSelectDto.getOrgId()), CorePost::getOrgId, postSelectDto.getOrgId())
                .orderByDesc(CorePost::getSort);

        IPage page = new Page(postSelectDto.getPageNo(), postSelectDto.pageSize);
        IPage data = postMapper.selectPage(page, postQuery);

        return data;
    }

    /**
     * 获取部门下所有职位
     *
     * @param orgId
     * @return
     */
    @Override
    public List<CorePost> getPostListByOrgId(String orgId) {
        LambdaQueryWrapper postQuery = Wrappers.<CorePost>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(orgId), CorePost::getOrgId, orgId)
                .eq(CorePost::getStatus, "0")
                .orderByDesc(CorePost::getSort);

        List data = postMapper.selectList(postQuery);
        return data;
    }


}
